package com.zzxx.photograph.web.servlet;

import com.zzxx.photograph.domain.Category;
import com.zzxx.photograph.domain.Img;
import com.zzxx.photograph.domain.PageBean;
import com.zzxx.photograph.service.CategoryService;
import com.zzxx.photograph.service.impl.CategoryServiceImpl;
import com.zzxx.photograph.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/CategoryServlet/*")
public class CategoryServlet extends BaseServlet {
    private CategoryService categoryService = new CategoryServiceImpl();

    public void findAllCid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Category> categoryList = categoryService.findAll();
        writeValue(resp, categoryList);
    }

    public void queryPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String pageSize = req.getParameter("pageSize");
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "4";
        }
        PageBean<Img> imgPageBean = categoryService.findImgByCid(Integer.parseInt(cid), Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        writeValue(resp, imgPageBean);
    }

    public void relateImg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _iid = req.getParameter("iid");
        int iid = Integer.parseInt(_iid);
        int cid = categoryService.findCidImgByIid(iid);
        List<Img> imgList = categoryService.findImgsByCid(cid,iid);
        writeValue(resp, imgList);
    }
}
